package com.kuns.subject.domian.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kuns.subject.common.enums.IsDeletedFlagEnum;
import com.kuns.subject.domian.convert.SubjectLabelConverter;
import com.kuns.subject.domian.entity.SubjectLabelBO;
import com.kuns.subject.domian.service.SubjectLabelDomainService;
import com.kuns.subject.infra.basic.entity.SubjectLabel;
import com.kuns.subject.infra.basic.entity.SubjectMapping;
import com.kuns.subject.infra.basic.service.SubjectLabelService;
import com.kuns.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.subjectLabelBO:{}", JSONObject.toJSONString(subjectLabelBO));
        }
        // 将BO转换成基础设施层的entity
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToEntity(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    /**
     * 更新题目标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.subjectLabelBO:{}", JSONObject.toJSONString(subjectLabelBO));
        }
        // 将BO转成infra层需要的entity
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToEntity(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 删除题目标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.delete.subjectLabelBO:{}", JSONObject.toJSONString(subjectLabelBO));
        }
        // 将BO转成infra层的entity
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToEntity(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.code);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 根据分类id查询题目标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.subjectLabelBO:{}", JSONObject.toJSONString(subjectLabelBO));
        }
        // 获取分类id
        Long categoryId = Long.parseLong(subjectLabelBO.getCategoryId());
        // 构造subjectMapping对象查询出所以的标签id
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        }
        // 拿出所有的标签id
        List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        // 查询出所有的标签
        List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
        // 将entity转成bo
        List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter.INSTANCE.convertEntityListToBoList(subjectLabelList);
        return subjectLabelBOList;
    }
}
